from django.conf import settings
from langchain.agents import create_react_agent, AgentExecutor
from langchain_community.llms.tongyi import Tongyi
from langchain_core.tools import Tool
from llama_index.core import ChatPromptTemplate
from django.core.mail import send_mail
from rest_framework.response import Response
from rest_framework.views import APIView

llm = Tongyi()




# 关于订单查询
def search_order(input):
    return "给你发布一个邮件"


# 近期活跃用户
def recommend_product(input):
    return "送优惠券"


def daq(input):
    return "额回答了，你的问题，下次再见"


tools = [
    Tool(func=search_order, name="search_order", description="高价值用户"),
    Tool(func=recommend_product, name="recommend_product", description="近期活跃用户"),
    Tool(func=daq, name="daq", description="咨询答疑，请用这个工具")
]
template = '''请尽可能回答以下问题。您可以使用以下工具:

            {tools}

           使用以下格式:

            Question: the input question you must answer
            Thought: you should always think about what to do
            Action: the action to take, should be one of [{tool_names}]
            Action Input: the input to the action
            Observation: the result of the action
            ... (this Thought/Action/Action Input/Observation can repeat N times)
            Thought: I now know the final answer
            Final Answer: the final answer to the original input question

            Begin!

            Question: {input}
            Thought:{agent_scratchpad}'''

prompt = ChatPromptTemplate.from_template(template)
agent = create_react_agent(prompt=prompt, tools=tools, llm=llm)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
